package cn.e2win.biz.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.e2win.biz.security.UserSession;



@Service
public class Dict{
	public static final String baseUpload = "http://img.whzxqy.cn";
	
	private static String realPath = null;
	
	
	
	
	public static String getRealPath() {
		return realPath;
	}
	public static void setRealPath(String realPath) {
		Dict.realPath = realPath;
	}
	
	public static String prefix(String type) {
		return baseUpload;
	}
	public static SysDictDao DictDao;
	public static Dict Instance = new Dict();
	public Dict(){
		System.err.println("in Dict============");
	}
	public static Map<String, Object> DictMap = new HashMap<String, Object>();
	
	public static List<SimpleMap> getList(String type){
		List<SimpleMap> list=null;
		if(UserSession.getUser()!=null){
			Integer coId= UserSession.getUser().getCoId();
			list = (List<SimpleMap>)DictMap.get("LIST_" + type + "_" + coId);
		}
		if (list == null){
			list =(List<SimpleMap>)DictMap.get("LIST_" + type);
		}
		if (list == null){
			list = new ArrayList<SimpleMap>();
		}
		return list;
	}
	public static Map<String, String> getMap(String type){
/*		Integer coId= UserSession.getUser().getCoId();
		if(!DictMap.containsKey("MAP_"+type)){
			DictMap.put("MAP_"+type, simpMap2Map(getList(type)));
		}*/
		Map<String, String> map = (Map<String, String>)DictMap.get("MAP_"+type);
		if(map==null){
			map = new HashMap<String, String>();
		}
		return map;
	}
	public static Object getAlias(String type, String key){
		if(key == null || "".equals(key))return "";
		Object name = getMap(type).get(key);
		if(name==null){
			name = key;
		}
		return name;
	}
	
	

	/**
	 * <DL>
	 * <DT><B>Method概要：将List转化为Map</B></DT>
	 * <DD>约定1，List中保存的是Map<String,Object></DD><BR>
	 * <DD>约定2，Map中有两个键值对，一个的key为"KEY",另一个的key为"VALUE"</DD><BR>
	 * </DL>
	 * @param list
	 * @return
	*/
	public static Map<String, String> simpMap2Map(final List<SimpleMap> list){
		Map<String, String> returnMap = new HashMap<String,String>(){
			{
				if(list != null){
					for (SimpleMap map : list) {				
						this.put(map.getKEY(), map.getVALUE());
					}
				}
			}
		};
		return returnMap;
	}


/*	public static Map<String,HashMap<String,String>> simpMap2Map(final Map<String,List<SimpleMap>> list){
		List<SimpleMap> tempList = null;
		Map<String,HashMap<String,String>> returnMap = new HashMap<String,HashMap<String,String>>(){
			{
				for(String key :list.keySet()){
					HashMap<String,String> tempMap = new HashMap<String,String>();
					if(list.get(key) != null){
						for (SimpleMap map : list.get(key)) {				
							tempMap.put(map.getKEY(), map.getVALUE());
						}
					}
					this.put(key.replaceFirst("LIST", "MAP"), tempMap);
				}
			}
		};
		return returnMap;
		//return null;
	}*/
	
	public static void initDict(SysDictDao dictDao){
		DictDao = dictDao;
		DictMap = DictDao.initDict();
		DictMap.put("COMPANY_USER_LIST", DictDao.getCompanyUserList(false));
		DictMap.put("COMPANY_ACTIVE_USER_LIST", DictDao.getCompanyUserList(true));
	}

	public static Object get(Object key){
		return DictMap.get(key);
	}
	public static void refresh(String type){
		/*Map<String,List<SimpleMap>> list = DictDao.getDictList(type);
		DictMap.putAll(list);
		DictMap.putAll(simpMap2Map(list));*/
		DictMap.putAll(DictDao.getDictListAndMap(type));
	}
}
